package com.kmust.dream.wanderingdream.donation.controller;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kmust.dream.wanderingdream.donation.entity.LearningstatusInformationDonationEntity;
import com.kmust.dream.wanderingdream.donation.service.LearningstatusInformationDonationService;
import com.kmust.dream.wanderingdream.comm.utils.PageUtils;
import com.kmust.dream.wanderingdream.comm.utils.R;



/**
 * 
 *
 * @author Leng
 * @email Leng81605@gmail.com
 * @date 2021-07-12 21:35:52
 */
@RestController
@Api(tags = "受捐赠者学习情况")
@RequestMapping("donation/learn")
public class LearningstatusInformationDonationController {
    @Autowired
    private LearningstatusInformationDonationService learningstatusInformationDonationService;

    /**
     * 列表
     */
    @GetMapping("/list")

    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = learningstatusInformationDonationService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/getlist/{recipientId}")
    public List<LearningstatusInformationDonationEntity> getList(@PathVariable("recipientId") String recipientId){
        List<LearningstatusInformationDonationEntity> list = learningstatusInformationDonationService.getResultsList(recipientId);
        for (int i = 0;i<list.size();i++){
            Map<String,String> remap = new HashMap<>();
            String result =list.get(i).getExaminationResults();
            String singlesub [] = result.split(",");
            for (int j =0;j<singlesub.length;j++){
                Pattern pattern = Pattern.compile("(.*):(\\w+)");
                Matcher matcher = pattern.matcher(singlesub[j]);
                if (matcher.find()){
                    remap.put(matcher.group(1), matcher.group(2));
                }
            }
            list.get(i).setExaminationResultsMap(remap);
        }
        return list;

    }
    @GetMapping("/getalllist")
    public R getAllList(@RequestParam Map<String, Object> params){
        PageUtils page = learningstatusInformationDonationService.queryPage(params);
        List<LearningstatusInformationDonationEntity> list = learningstatusInformationDonationService.getAllList();
        for (int i = 0;i<list.size();i++){
            Map<String,String> remap = new HashMap<>();
            String result =list.get(i).getExaminationResults();
            if (result == null){
                continue;
            }
            String singlesub [] = result.split("[,|，]");
            for (int j =0;j<singlesub.length;j++){
                Pattern pattern = Pattern.compile("(.*):(\\w+)");
                Matcher matcher = pattern.matcher(singlesub[j]);
                if (matcher.find()){
                    remap.put(matcher.group(1), matcher.group(2));
                }

            }
            list.get(i).setExaminationResultsMap(remap);
        }
        page.setList(list);
        return R.ok().put("page",page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{lsid}")

    public R info(@PathVariable("lsid") Integer lsid){
		LearningstatusInformationDonationEntity learningstatusInformationDonation = learningstatusInformationDonationService.getById(lsid);
		Map<String,String> remap = new HashMap<>();
		String result =learningstatusInformationDonation.getExaminationResults();
		if (result == null){
		    return R.ok("没有成绩存在!!");
		}
		String singlesub [] = result.split("[,|，]");
		for (int j =0;j<singlesub.length;j++){
		    Pattern pattern = Pattern.compile("(.*):(\\w+)");
		    Matcher matcher = pattern.matcher(singlesub[j]);
		    if (matcher.find()){
		        remap.put(matcher.group(1), matcher.group(2));
		    }
		}
		learningstatusInformationDonation.setExaminationResultsMap(remap);
        return R.ok().put("learningstatusInformationDonation", learningstatusInformationDonation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存学习情况")
    public R save(LearningstatusInformationDonationEntity learn){
		learningstatusInformationDonationService.save(learn);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")

    public R update(LearningstatusInformationDonationEntity learningstatusInformationDonation){
        System.out.println(learningstatusInformationDonation);
		learningstatusInformationDonationService.updateById(learningstatusInformationDonation);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")

    public R delete(@RequestBody Integer[] lsids){
		learningstatusInformationDonationService.removeByIds(Arrays.asList(lsids));

        return R.ok();
    }

}
